package com.zhuozhou.jdyc.main.settlement.activity;

import android.os.Bundle;
import android.os.CountDownTimer;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityVerificationBinding;
import com.zhuozhou.jdyc.main.main.viewmodel.MainViewModel;
import com.zhuozhou.jdyc.main.settlement.adapter.VerificationAdapter;
import com.zhuozhou.jdyc.main.settlement.bean.VerificationBean;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.wight.dialog.SeeImageDialog;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/10/27
 * Describe:核销订单
 */
public class VerificationActivity extends BaseActivity<MainViewModel, ActivityVerificationBinding> {

    TimeCount timer;
    private VerificationAdapter verificationAdapter;
    private SeeImageDialog seeImageDialog;
    private String id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_verification;
    }

    @Override
    public String setTitleBar() {
        return "核销订单";
    }

    @Override
    public void initView() {
        mViewModel.MyPayMutableLiveData.observe(this, v -> {
            List<VerificationBean.ListBeanX> lists = new ArrayList<>();
            for (VerificationBean.ListBeanX list : v.getList()) {
                if (list.getDistribution() == 2 && list.getStatus() == 1 && list.getShoppingtype() == 0 && list.getUsertype() == 0) {
                    lists.add(list);
                }
            }
            verificationAdapter = new VerificationAdapter(this);
            verificationAdapter.setList(lists);
            verificationAdapter.setOnItemSetMoneyListener((str, id) -> {
                seeImageDialog = new SeeImageDialog(this, str);
                seeImageDialog.builder().show();
                this.id = id;
                timer = new TimeCount(600000, 2000);
                timer.start();
            });
            binding.mRecyclerViewShop.setAdapter(verificationAdapter);
            binding.mRecyclerViewShop.setLayoutManager(new MyLayoutManager(this));
        });
        mViewModel.HeXaoMutableLiveData.observe(this, v -> {
            if (v.getType() == 1) {
                timer.onFinish();
                seeImageDialog.dismiss();
                ToastUtils.showShort(v.getMsg());
                mViewModel.getMyPay(UserAccountHelper.getUserID());
            }
        });
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getMyPay(UserAccountHelper.getUserID());
    }

    /**
     * 获取验证码倒计时
     */
    class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {//计时完毕时触发
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            if (StringUtil.isNotEmpty(id) && seeImageDialog.isShowing())
                mViewModel.getHeXao(id);
        }
    }
}
